<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class UserIncome extends Model
{
    //用户收入记录表
    const CREATED_AT = 'insert_time';
    /**
     * 与模型关联的数据表。
     *
     * @var string
     */
    protected $table = 'lexiang_income_log';

    protected $primaryKey = 'income_id';

    public $timestamps = false;

    /**
     * 可以被批量赋值的属性。
     *
     * @var array
     */
    protected $fillable = ['users_id','income_value','source','bewrite','insert_time'];

    public function add($data)
    {
        $this->income_value = $data['income_value'];
        $this->users_id = $data['users_id'];
        $this->source = $data['source'];
        $this->bewrite = $data['bewrite'];
        $this->insert_time = date('Y-m-d H:i:s');
        $userinfo = User::where('user_id',$data['users_id'])->first();
        $userinfo->account = $userinfo->account+$data['income_value'];
        $userinfo->save();
        if($this->save()){
            return [
                'success'=>true,
                'code'=>'000',
                'message'=>"收款记录,保存成功",
                "account"=>$userinfo->account
            ];
        }else{
            return [
                'success'=>false,
                'code'=>'006',
                'message'=>"收款记录保存出错，请稍后再试！"
            ];
        }
    }
}
